#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdio>
#include <unistd.h>
#include <string.h>
using namespace std;
int main()
{
    pid_t pid = fork();
    if (pid == -1)
    {
        perror("fork");
        return -1;
    }
    else if (pid == 0)
    {
        for (int i = 1; i <= 10; i++)
        {
            printf("我是子进程 getpid==%d, 打印第%d次\n", getpid(), i);
            sleep(1);
        }
        exit(5);
    }
    printf("父进程等待子进程退出，回收其资源\n");
    int status = 0;
    int ret = wait(&status);
    if (ret == -1)
    {
        perror("wait");
        return 3;
    }
    if (WIFEXITED(status))
    {
        printf("子进程退出状态码：%d\n", WEXITSTATUS(status));
    }
    else if(WIFSIGNALED(status)){
        printf("子进程被杀死...：%d\n", WTERMSIG(status));
    }
    else if(WIFSTOPPED(status)){ 
        printf("子进程被暂停...：%d\n", WSTOPSIG(status));
    }
    return 0;
}